
package br.com.aeternu.controleatendimento.manutencao.conclusao;

import java.awt.Rectangle;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import org.apache.log4j.Logger;
import br.com.aeternu.controleatendimento.common.ui.ControleLista;
import br.com.aeternu.controleatendimento.common.ui.EasyJFrame;
import br.com.aeternu.controleatendimento.common.ui.EasyOptionPane;
import br.com.aeternu.controleatendimento.excel.conclusao.ConclusaoBean;
import br.com.aeternu.controleatendimento.excel.conclusao.ExcelConclusao;
import br.com.aeternu.controleatendimento.exception.ControleAtendimentoException;
import br.com.aeternu.controleatendimento.manutencao.conclusao.ui.IfcUIConclusao;
import br.com.aeternu.controleatendimento.manutencao.conclusao.ui.UIConclusao;

public class Conclusao extends ControleLista implements IfcUIConclusao
{

   private static Logger logger = Logger.getLogger(Conclusao.class);
   private ExcelConclusao excel;
   private UIConclusao ui;

   public Conclusao() throws ControleAtendimentoException
   {
      this.excel = new ExcelConclusao();
      this.ui = new UIConclusao(this);
      carregaTela();
      atualizaList();
   }

   @Override
   public void atualizaPlanilha(EasyJFrame ui, int row, int column, Object newValue)
   {
      UIConclusao uiConclusao = (UIConclusao) ui;

      try
      {
         List<ConclusaoBean> conclusaoBeanList = excel.obterRegistros();
         ConclusaoBean bean;

         if (!conclusaoBeanList.isEmpty())
         {
            try
            {
               bean = conclusaoBeanList.get(row);
            }
            catch (IndexOutOfBoundsException e)
            {
               // Lista não está vazia, porém o índice solicitado (row) ainda não foi preenchido
               bean = new ConclusaoBean();
            }
         }
         else
         {
            bean = new ConclusaoBean();
         }

         switch (column)
         {
            case 0:
               bean.setConclusao((String) newValue);
               break;
         }

         excel.atualiza(bean);
         atualizaList();
      }
      catch (ControleAtendimentoException e)
      {
         logger.error(e.getMessage(), e);
         EasyOptionPane.showErrorMessageDialog(uiConclusao, e.getMessage());
      }
   }

   @Override
   public void incluirConclusao()
   {
      DefaultTableModel model = (DefaultTableModel) ui.getTable().getModel();
      model.addRow(new Object[]{""});

      int row = model.getRowCount() - 1;

      Rectangle rect = ui.getTable().getCellRect(row, model.getColumnCount(), true);
      ui.getTable().addRowSelectionInterval(row, row);
      ui.getTable().scrollRectToVisible(rect);
      ui.getTable().requestFocusInWindow();
   }

   protected void atualizaList() throws ControleAtendimentoException
   {
      DefaultTableModel model = (DefaultTableModel) ui.getTable().getModel();

      while (model.getRowCount() > 0)
      {
         for (int i = 0; i < model.getRowCount(); i++)
         {
            model.removeRow(i);
         }
      }

      List<ConclusaoBean> conclusaoBeanList = excel.obterRegistros();

      for (ConclusaoBean bean : conclusaoBeanList)
      {
         model.addRow(new Object[]{bean.getConclusao()});
      }

      // int row = model.getRowCount() - 1;
      //
      // Rectangle rect = ui.getTable().getCellRect(row, model.getColumnCount(), true);
      // ui.getTable().addRowSelectionInterval(row, row);
      // ui.getTable().scrollRectToVisible(rect);
      // ui.getTable().requestFocusInWindow();
   }

   protected void carregaTela()
   {
      // Nada para carregar
   }

}
